package com.easy.frame.base.api.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;


/**
 * @author tp
 */
public class IPUtils {
    private final static String IP_SEPARATOR = ",";
    private final static String UNKNOWN = "unknown";

    /**
     * return 机器名
     */
    public static String getHostName() {
        try {
            InetAddress ia = InetAddress.getByName("127.0.0.1");
            return ia.getHostName();
        } catch (UnknownHostException e) {
            return null;
        }
    }

    /**
     * 判断是否开发或测试机 (Hard Code)
     */
    public static boolean isDev() {
        String[] ips = getIps();
        if (ips == null) {
            return false;
        }
        for (String ip : ips) {
            if (ip.startsWith("10.10")) {
                return true;
            }
            if (ip.startsWith("192.168")) {
                return true;
            }
        }
        return false;
    }


    /**
     * 获取客户端Ip
     *
     * @param request 请求
     */
    public static String getIP(HttpServletRequest request) {

        String ip = request.getHeader("X-Real-IP");

        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        } else {
            return ip;
        }

        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        } else {
            // 当有多级反向代理时，x-forwarded-for值为多个时取第一个ip地址
            if (ip.contains(IP_SEPARATOR)) {
                ip = ip.substring(0, ip.indexOf(IP_SEPARATOR));
            }
            return ip;
        }

        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        } else {
            return ip;
        }

        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        } else {
            return ip;
        }

        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = "";
        }
        return ip;
    }

    /**
     * 获取当前机器的所有IP
     */
    public static String[] getIps() {

        List<String> ips = new ArrayList<>();
        try {
            Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
            for (NetworkInterface intents : Collections.list(nets)) {
                if (intents.getHardwareAddress() != null) {
                    List<InterfaceAddress> list = intents.getInterfaceAddresses();
                    for (InterfaceAddress interfaceAddress : list) {
                        String localIp = interfaceAddress.getAddress().getHostAddress();
                        if (StringUtils.isNotBlank(localIp)) {
                            ips.add(localIp);
                        }
                    }
                }
            }
        } catch (SocketException e1) {
            return null;
        }
        return ips.toArray(new String[0]);
    }
}
